package com.tjsinfo.tjsoa.Dao;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.LogicalExpression;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Projection;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.hibernate.criterion.SimpleExpression;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.tjsinfo.tjsoa.common.PaginationSupport;

public class QbcDao extends HibernateDaoSupport implements IQbcDao{
	
	public List getCriteria(DetachedCriteria criteria) {
		
		List list = this.getHibernateTemplate().findByCriteria(criteria);
		 return list;
	}
	public List getLogicalExpression(Class class1,LogicalExpression expression) {
		 DetachedCriteria criteria = DetachedCriteria.forClass(class1);
		 criteria.add(expression);
		 List list = this.getHibernateTemplate().findByCriteria(criteria);
		 return list;
	}
	public List getSimpleExpression(Class class1,SimpleExpression expression) {
		 DetachedCriteria criteria = DetachedCriteria.forClass(class1);
		 criteria.add(expression);
		 List list = this.getHibernateTemplate().findByCriteria(criteria);
		 return list;
	}
	public List getOneFuHeLogicalExpression(Class zhuclass,String fuclass,LogicalExpression expression) {
		DetachedCriteria criteria = DetachedCriteria.forClass(zhuclass);
		DetachedCriteria criteria2 = criteria.createCriteria(fuclass);
		criteria2.add(expression);
		List list = this.getHibernateTemplate().findByCriteria(criteria);
		return list;
	}
	public List getOneFuHeLogicalExpression(Class zhuclass,LogicalExpression expression,String fuclass,LogicalExpression expression1) {
		DetachedCriteria criteria = DetachedCriteria.forClass(zhuclass);
		criteria.add(expression);
		DetachedCriteria criteria2 = criteria.createCriteria(fuclass);
		criteria2.add(expression1);
		List list = this.getHibernateTemplate().findByCriteria(criteria);
		return list;
	}
	public List getOneFuHeSimpleExpressio(Class zhuclass,String fuclass,SimpleExpression expression) {
		DetachedCriteria criteria = DetachedCriteria.forClass(zhuclass);
		DetachedCriteria criteria2 = criteria.createCriteria(fuclass);
		criteria2.add(expression);
		List list = this.getHibernateTemplate().findByCriteria(criteria);
		return list;
	}
	public List getOneFuHeSimpleExpressio(Class zhuclass,SimpleExpression expression,String fuclass,SimpleExpression expression1) {
		DetachedCriteria criteria = DetachedCriteria.forClass(zhuclass);
		criteria.add(expression);
		DetachedCriteria criteria2 = criteria.createCriteria(fuclass);
		criteria2.add(expression1);
		List list = this.getHibernateTemplate().findByCriteria(criteria);
		return list;
	}
	public List getTwoFuHeLogicalExpression(Class zhuclass,String fuclass1,LogicalExpression expression1,String fuclass2,LogicalExpression expression2) {
		DetachedCriteria criteria = DetachedCriteria.forClass(zhuclass);
		DetachedCriteria criteria2 = criteria.createCriteria(fuclass1);
		criteria2.add(expression1);
		DetachedCriteria criteria3 = criteria.createCriteria(fuclass2);
		criteria3.add(expression2);
		List list = this.getHibernateTemplate().findByCriteria(criteria);
		return list;
	}
	public List getTwoFuHeLogicalExpression(Class zhuclass,LogicalExpression expression,String fuclass1,LogicalExpression expression1,String fuclass2,LogicalExpression expression2) {
		DetachedCriteria criteria = DetachedCriteria.forClass(zhuclass);
		criteria.add(expression);
		DetachedCriteria criteria2 = criteria.createCriteria(fuclass1);
		criteria2.add(expression1);
		DetachedCriteria criteria3 = criteria.createCriteria(fuclass2);
		criteria3.add(expression2);
		List list = this.getHibernateTemplate().findByCriteria(criteria);
		return list;
	}
	
	public int getQueryNumber(DetachedCriteria criteria,String propertyName){
		criteria.setProjection(Projections.count(propertyName));
		List list = this.getHibernateTemplate().findByCriteria(criteria);
		return (Integer)list.get(0);
	}
	
	public PaginationSupport findPageByCriteria(final DetachedCriteria detachedCriteria,final int pageSize,final int startIndex){   
        return (PaginationSupport) getHibernateTemplate().execute(new HibernateCallback() {   
            public Object doInHibernate(Session session) throws HibernateException {
                Criteria criteria = detachedCriteria.getExecutableCriteria(session);           
                int totalCount = ((Integer) criteria.setProjection(Projections.rowCount()).uniqueResult()).intValue();   
                criteria.setProjection(null);
                List items = criteria.setFirstResult(startIndex).setMaxResults(pageSize).list();
                PaginationSupport ps = new PaginationSupport(items, totalCount, pageSize, startIndex);
                return ps;
            }   
        }, true);   
    }
	
	public PaginationSupport findPageByCriteria(final DetachedCriteria detachedCriteria,final List<Order> orderList,final int pageSize,final int startIndex){   
        return (PaginationSupport) getHibernateTemplate().execute(new HibernateCallback() {   
            public Object doInHibernate(Session session) throws HibernateException {   
                Criteria criteria = detachedCriteria.getExecutableCriteria(session);
                int totalCount = ((Integer) criteria.setProjection(Projections.rowCount()).uniqueResult()).intValue();
                
                //Projection count=Projections.rowCount();
                //Criteria test = criteria.setProjection(count);
                //int totalCount1=((Number)test.uniqueResult()).intValue();
               
                for (Order order: orderList) {
					criteria.addOrder(order);
				}
	            criteria.setProjection(null);
	            criteria.setMaxResults(pageSize);
	            criteria.setFirstResult((startIndex-1)*pageSize);
				List items = criteria.setFirstResult(startIndex).setMaxResults(pageSize).list();
                PaginationSupport ps = new PaginationSupport(items, totalCount, pageSize, startIndex);
                return ps;
            }   
        }, true);   
    }
	public Object getObjectById(Class zhuclass,Object id) {
		DetachedCriteria criteria = DetachedCriteria.forClass(zhuclass);
		SimpleExpression b = Restrictions.eq("id", id);
		criteria.add(b) ;
		List list = this.getHibernateTemplate().findByCriteria(criteria);
		return list.get(0);
	}

	public void saveObject(Object object) {
		try {
			 getHibernateTemplate().save(object);
		} catch (RuntimeException re) {
			throw re;
		}
	}
	public Integer save(Object object) {
		Integer i = 0;
		try {
			i = (Integer) getHibernateTemplate().save(object);
		} catch (RuntimeException re) {
			throw re;
		}
		return i;
	}
	public void update(Object object) {
		try {
			getHibernateTemplate().update(object);
		} catch (RuntimeException re) {
			throw re;
		}
	}
	public void delete(Object object) {
		try {
			getHibernateTemplate().delete(object);
		} catch (RuntimeException re) {
			throw re;
		}
	}
}
